﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Gpsys.Entity.MSG;
using Gpsys.Web.Business;
using System.Web.Mvc;

namespace Gpsys.Web.Models
{
    public class RoleEntityModel
    {
        public Role Role { get; set; }
    }

    public class RoleModel
    {
        public bool InsertRole(Role user)
        {
            RoleBusiness bs = new RoleBusiness();

            return bs.InsertRole(user);
        }

        public bool UpdateRole(Role user)
        {
            RoleBusiness bs = new RoleBusiness();

            return bs.UpdateRole(user);
        }

        public Role GetRole(int Id)
        {
            RoleBusiness bs = new RoleBusiness();

            return bs.GetRole(Id);
        }

        public void DeleteRole(int Id)
        {
            RoleBusiness bs = new RoleBusiness();

            bs.DeleteRole(Id);
        }

        private List<Role> m_users;

        public List<Role> Roles
        {
            get
            {
                if (m_users == null)
                {
                    RoleBusiness bs = new RoleBusiness();
                    m_users = bs.GetRoles();
                }
                return m_users;
            }
        }

        public void ValidateRole(ModelStateDictionary modelState, RoleEntityModel model, bool isCreate)
        {
            if (isCreate && string.IsNullOrEmpty(model.Role.RoleName))
                modelState.AddModelError("Role.RoleName", "RoleName could not be empty.");

            if (string.IsNullOrEmpty(model.Role.Name))
                modelState.AddModelError("Role.Name", "Name could not be empty.");

            RoleBusiness bs = new RoleBusiness();
            if (bs.CheckCodeUsed(model.Role))
            {
                modelState.AddModelError("Role.RoleName", "RoleName is already been used.");
            }
        }
    }
}